// pages/subscribe-detail/subscribe-detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trafficTypePicker:{
      value: 0,
      array:[
        '不限',
        '飞机',
        '火车',
        '地铁',
        '长途客车/大巴',
        '公交车',
        '出租车',
        '轮船',
        '其它公共场所'
      ]
    },
    siteTitleText:[
      '场所',
      '航班',
      '车次',
      '路线号',
      '车牌',
      '路线号',
      '车牌',
      '班次',
      '地点'
    ],
    datePicker:{
      value:"",
      showText:"不限",
      setEmpty:true
    },
    editable: true,
    stateType:[
      "推送提醒时出错",
      '订阅中',
      '已提醒',
      '已取消',
    ]
  },

  // 数据
  value: {
    regionInputText:'',
    siteInputText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    // 默认是添加订阅状态

    if(app.subscribePage.type != 'add'){
      var datePicker = this.data.datePicker
      var trafficTypePicker = this.data.trafficTypePicker
      
      if(app.subscribePage.subscribe.date!=null && app.subscribePage.subscribe.date!=""){
        datePicker.value = app.subscribePage.subscribe.date
        datePicker.showText = datePicker.value
        datePicker.setEmpty = false
      }
  
      trafficTypePicker.value = app.subscribePage.subscribe.type
  
      this.setData({
        type: app.subscribePage.type,
        editable: false,
        datePicker: datePicker,
        trafficTypePicker: trafficTypePicker,
        site:app.subscribePage.subscribe.site,
        state:app.subscribePage.subscribe.state
      })
    }

    console.log(app.subscribePage)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 绑定日期选择器改变事件
  bindDatePickerChange: function (e) {
    var datePicker = this.data.datePicker
    datePicker.value = e.detail.value
    datePicker.showText = e.detail.value
    datePicker.setEmpty = false
    this.setData({
      datePicker : datePicker
    })
  },

  // 绑定日期选择器清空按钮点击事件
  bindDatePickerSetEmptyTap: function () {
    var datePicker = this.data.datePicker
    datePicker.value = ''
    datePicker.showText = '不限'
    datePicker.setEmpty = true
    this.setData({
      datePicker : datePicker
    })
  },

  // 绑定地区输入框输入事件
  bindRegionInput: function (e) {
    this.value.regionInputText = e.detail.detail.value
  },

  // 绑定地区选择器改变事件
  bindRegionPickerChange: function (e) {
    var regionPicker = this.data.regionPicker
    // 保存值
    regionPicker.value = e.detail.value
    // 处理显示文字 保存code
    if(e.detail.value[0]=='全部'){
      regionPicker.showText = '全国'
      regionPicker.code='000000'
    }else{
      regionPicker.showText = e.detail.value[0] + 
        ( e.detail.value[1] == '全部' ? '': (
          ' ' + e.detail.value[1] + (
            e.detail.value[2] == '全部' ? '' : ' ' + e.detail.value[2]
          )
        ) )
      regionPicker.code = e.detail.code[e.detail.code.length-1]
    }
    this.setData({
      regionPicker:regionPicker
    })
  },

  // 绑定出行类型选择器改变事件
  bindTrafficTypePickerChange: function (e) {
    var trafficTypePicker = this.data.trafficTypePicker
    trafficTypePicker.value = e.detail.value
    this.setData({
      trafficTypePicker : trafficTypePicker
    })
  },

  // 绑定场所输入框输入事件
  bindSiteInput: function (e) {
    this.value.siteInputText = e.detail.detail.value
  },

  // 执行订阅
  toSubscribe: function(e){
    var date = this.data.datePicker.value
    var region = this.value.regionInputText
    var type = this.data.trafficTypePicker.value
    var site = this.value.siteInputText

    // console.log('日期：' + this.data.datePicker.value)
    // console.log('地区：' + this.value.regionInputText)
    // console.log('类型：' + this.data.trafficTypePicker.value)
    // console.log('场所：' + this.value.siteInputText)

    // wx.navigateTo({
    //   url: '/pages/search-result-list/search-result-list?data=' + data + '&region=' + region + '&type=' + type + '&site=' + site,
    // })

    var tmplId = 'AdVXBEBHjwgj_x1YFUoO-vLaaI48B3LfFoycugMc3nQ'

    wx.requestSubscribeMessage({
      tmplIds:[tmplId],
      success(res){
        var result = res[tmplId]
        if( result == 'accept'){
          // 订阅成功
          console.log("订阅成功")
          // 执行后台订阅记录操作
          
          wx.showLoading({
            title: '添加订阅中',
          })

          wx.request({
            url: app.subscribeSetApi,
            data:{
              date:date,
              site:site,
              type:type,
              wxmsgTemplateId:tmplId,
              token:app.token
            },
            success(res){
              if(res.data.code==0){
                wx.hideLoading({
                  complete: (res) => {},
                })
                wx.showToast({
                  title: '订阅成功',
                  icon:'success',
                  duration:800,
                  success(res){
                    setTimeout(() => {
                      wx.navigateBack({
                        data:1,
                        complete: (res) => {},
                      })
                    }, 800);
                  }
                })
              }
            }
          })

        }else if(result == 'reject'){
          // 取消订阅
          console.log("取消订阅")
        }else {
          // 后台封禁
          console.log("后台封禁")
        }
      },
      fail(res){
        console.log('订阅失败')
        console.log(res.errCode)
        console.log(res.errMsg)
      }
    })

  },

  
  toCancelSubscribe: function (e) {
    wx.showModal({
      title:"取消订阅",
      content:"确定取消订阅吗？",
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '取消订阅中',
          })
          // 执行取消订阅
          var subscribe = app.subscribePage.subscribe
          wx.request({
            url: app.subscribeSetApi,
            data:{
              id: subscribe.id,
              date: subscribe.date,
              site: subscribe.site,
              type: subscribe.type,
              wxmsgTemplateId: subscribe.wxmsgTemplateId,
              token:app.token,
              state:3
            },
            success(res){
              if(res.data.code==0){
                wx.hideLoading({
                  complete: (res) => {},
                })
              }
              wx.showToast({
                title: '已取消订阅',
                icon:'success',
                duration:800,
                success(res){
                  setTimeout(() => {
                    wx.navigateBack({
                      data:1,
                      complete: (res) => {},
                    })
                  }, 800);
                }
              })
            }
          })
        }
      }
    })


    
  }
})